package com.qdxwx.data.service.favorite;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.favorite.FavoriteMapper;
import com.qdxwx.models.favorite.Favorite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class FavoriteServiceImpl implements FavoriteService {

	@Autowired
	private FavoriteMapper favoriteMapper;

	@Override
	public void insert(Favorite favorite) {
		favorite.setAddTime(LocalDateTime.now());
		favoriteMapper.insert(favorite);
	}

	@Override
	public Favorite get(int id) {
		if (id < 1) return null;
		return favoriteMapper.selectById(id);
	}

	@Override
	public int delete(int id) {
		return favoriteMapper.deleteById(id);
	}

	@Override
	public int delete(Favorite favorite) {
		return favoriteMapper.deleteByFavorite(favorite);
	}

	@Override
	public boolean isExist(long userId, int courseId, int typeId) {
		return favoriteMapper.isExist(userId, courseId, typeId) > 0;
	}

	@Override
	public List<Favorite> getList(long userId, int typeId, PageRequest page) {
		if (userId < 1) return null;
		return favoriteMapper.selectList(userId, typeId, page);
	}

	@Override
	public int count(long userId, int typeId) {
		if (userId < 1) return 0;
		return favoriteMapper.count(userId, typeId);
	}

	@Override
	public int updateCombine(long userId1, long userId2) {
		if(userId1 < 1 || userId2 < 1){
			throw new RuntimeException("更新用户收藏：参数错误");
		}
		return favoriteMapper.updateCombine(userId1, userId2);
	}
}